﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSequence;
using Ini;
using System.Data.OleDb;

namespace Sequence
{
    public partial class MDISequence : Form
    {
        private int childFormNumber = 0;
        public OleDbConnection m_connection;

        public MDISequence()
        {
            InitializeComponent();

            IniFile ini = new IniFile("C:\\Sapiens\\Sequence\\Sequence.ini");
            String sMRUPath = "";
            for (int i = 1; i < 100; i++)
            {
                sMRUPath = ini.IniReadValue("MRU", "Path" + i.ToString());
                if (sMRUPath == "")
                {
                    break;
                }
                else
                {
                    var item = new ToolStripMenuItem(sMRUPath);
                    item.Tag = sMRUPath;
                    item.Click += OpenRecentFile;
                    recentFoldersToolStripMenuItem.DropDownItems.Add(item);
                }
            }
        }

        public OleDbConnection GetConnection()
        {
            String sConn = GetConnectString();
            if (m_connection == null)
            {
                try
                {
                    m_connection = new System.Data.OleDb.OleDbConnection(sConn);
                    m_connection.Open();
                }
                catch (Exception ex)
                {
                    // To Do 
                }
            }
            return m_connection;
        }

        public String GetConnectString()
        {
            IniFile ini = new IniFile("C:\\Sapiens\\Sequence\\Sequence.ini");
            String sGetConnectString = ini.IniReadValue("Settings", "sConnectString");
            if (sGetConnectString == "")
                sGetConnectString = "Provider=OraOLEDB.Oracle;Data Source=uslt023;User ID=ALIS;Password=oracle;";
            ini.IniWriteValue("Settings", "sConnectString", sGetConnectString);
            return sGetConnectString;
        }


        void OpenRecentFile(object sender, EventArgs e)
        {
            var menuItem = (ToolStripMenuItem)sender;
            var filepath = (string)menuItem.Tag;
            
            FrmSequence2 childForm = new FrmSequence2();
            childForm.MdiParent = this;
            //childForm.Text = "Sequence " + childFormNumber++;
            childForm.Path = filepath;
            childForm.Show();
        }


        private void ShowNewForm(object sender, EventArgs e)
        {

            RunSql("insert into t_api_qd_process (ID, NAME) values ( (select nvl(max(id),0)+1 from t_api_qd_process ), 'New')");
            FrmSequence2 childForm = new FrmSequence2();
            childForm.MdiParent = this;
            childForm.Text = "Sequence " + childFormNumber++;
            childForm.sId = GetMaxProcess();
            childForm.Show();
               
        }
        private string GetMaxProcess()
        {

            string sSql = "select nvl(max(id),0) from t_api_qd_process";
            string sRet = "";
            try
            {
                OleDbCommand command = new OleDbCommand(sSql, m_connection);
                OleDbDataReader oDataReader = command.ExecuteReader();                
                while (oDataReader.Read())
                {
                    sRet = oDataReader[0].ToString();
                }
                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                //WriteError(ex.Message + " (" + sSql + ")");
            }
            return sRet;
        }




        private void RunSql(String sSql)
        {
            try
            {
                OleDbCommand command = new OleDbCommand(sSql, GetConnection());
                int iRows = command.ExecuteNonQuery();
                command.Dispose();
            }
            catch (Exception ex)
            {
                //rtbErrors.Text += ex.Message + " (" + sSql + ")\r\n";
            }
        }

        private void OpenFile(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();


            if (result == DialogResult.OK)
            {

                IniFile ini = new IniFile("C:\\Sapiens\\Sequence\\Sequence.ini");
                String sMRUPath = fbd.SelectedPath;
                for (int i = 1; i < 100; i++)
                {
                    string sIniMRUPath = ini.IniReadValue("MRU", "Path" + i.ToString());
                    if (sIniMRUPath == "")
                    {
                        ini.IniWriteValue("MRU", "Path" + i.ToString(), sMRUPath);
                        break;
                    }
                    else if (sMRUPath == sIniMRUPath)
                        break;
                }

                
                FrmSequence2 childForm = new FrmSequence2();
                childForm.MdiParent = this;
                childForm.Text = "Sequence " + childFormNumber++;
                childForm.Path = fbd.SelectedPath;
                childForm.Show();
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void MDISequence_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (FrmSequence2 childForm in MdiChildren)
            {
                childForm.Save();
            }
        }

        private void recentFoldersToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            foreach (FrmSequence2 childForm in MdiChildren)
            {
                childForm.Save();
            }
        }

        private void openDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            FrmOpenProcess oFrmOpenProcess = new FrmOpenProcess(GetConnection());
            oFrmOpenProcess.ShowDialog();

            if (oFrmOpenProcess.sId != null)
            {                
                FrmSequence2 childForm = new FrmSequence2();
                childForm.MdiParent = this;
                childForm.Text = "Sequence " + childFormNumber++;
                childForm.Path = "";
                childForm.sId = oFrmOpenProcess.sId;
                childForm.Show();
            }
        }
    }
}
